package com.fivestar.finance.app.ui.message;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.fivestar.finance.app.R;
import com.fivestar.finance.app.bean.AuthorInfo;

import java.util.Collections;
import java.util.List;

/**
 * 简要描述
 *  
 *  @author Derek.Kidd
 * @version V1.0.0
 * @date 2016/8/24 16:54
 */
public class AuthorRecyclerAdapter extends RecyclerView.Adapter<AuthorRecyclerAdapter.AuthorViewHolder> {

    private List<AuthorInfo> mAuthorInfoList;
    /**
     * item view 的类型是否是小类型的
     */
    private boolean mIsSmall = false;
    Context mContext;


    public AuthorRecyclerAdapter(Context context, List<AuthorInfo> authorInfoList) {
        this.mContext = context;
        this.mAuthorInfoList = authorInfoList;
    }

    public void setSmallType(boolean isSmall) {
        this.mIsSmall = isSmall;
    }

    @Override
    public AuthorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View childView = null;
        if (mIsSmall) {
            childView = inflater.inflate(R.layout.author_small_card_layout, parent, false);
        } else {
            childView = inflater.inflate(R.layout.author_card_layout, parent, false);
        }
        AuthorViewHolder viewHolder = new AuthorViewHolder(childView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(AuthorViewHolder holder, int position) {
        AuthorInfo authorInfo = mAuthorInfoList.get(position);

        //FIXME
        Glide.with(mContext)
                .load(authorInfo.getPortraitUrl())
                .placeholder(R.mipmap.ic_launcher) // 等待中图片
                .error(R.mipmap.ic_launcher)// 失败图片
                //.skipMemoryCache(true) //跳过内存缓存
                //.diskCacheStrategy(DiskCacheStrategy.NONE) //跳过硬盘缓存
                //.priority(Priority.HIGH)
                .crossFade()
                .into(holder.mPortraitView);
        /**
         *  图片的缩放，centerCrop()和fitCenter()
         * 缓存策略:
         * DiskCacheStrategy.NONE 什么都不缓存
         * DiskCacheStrategy.SOURCE 仅仅只缓存原来的全分辨率的图像
         * DiskCacheStrategy.RESULT 仅仅缓存最终的图像，即降低分辨率后的（或者是转换后的）
         * DiskCacheStrategy.ALL 缓存所有版本的图像（默认行为）
         * 优先级，设置图片加载的顺序:
         * Priority.LOW
         * Priority.NORMAL
         * Priority.HIGH
         * Priority.IMMEDIATE
         */

              /*.load()加载资源：1,drawable资源。2，本地File文件。3，uri。4，网络图片url
                .placeholder() 图片占位符，
                .error() 图片加载失败时显示
                .crossFade() 显示图片时执行淡入淡出的动画默认300ms
                .dontAnimate() 不执行显示图片时的动画
                .override() 设置图片的大小
                .centerCrop()和fitCenter() 图片的显示方式
                .animate() view动画 2个重构方法
                .transform() bitmap转换
                .bitmapTransform() bitmap转换。 比如:旋转，方法，缩小，高斯模糊等等（当用了转换后你就不能使用 .centerCrop() 或 .fitCenter() 了。）
                .priority(Priority.HIGH)// 当前线程的优先级
                .signature(new StringSignature(“ssss”))
                .thumbnail(0.1f) 缩略图，3个重构方法：优先显示原始图片的百分比(10%)
                .listener() 异常监听
                .into(); 将图片显示到控件，3个构造方法。*/
        holder.mNickNameView.setText(authorInfo.getNickName());
        holder.mMottoView.setText(authorInfo.getMotto());
    }

    @Override
    public int getItemCount() {
        if (mAuthorInfoList == null) {
            return 0;
        }
        return mAuthorInfoList.size();
    }

    /**
     * 移动Item
     *
     * @param fromPosition
     * @param toPosition
     */
    public void moveItem(int fromPosition, int toPosition) {
        //做数据的交换
        if (fromPosition < toPosition) {
            for (int index = fromPosition; index < toPosition; index++) {
                Collections.swap(mAuthorInfoList, index, index + 1);
            }
        } else {
            for (int index = fromPosition; index > toPosition; index--) {
                Collections.swap(mAuthorInfoList, index, index - 1);
            }
        }
        notifyItemMoved(fromPosition, toPosition);
    }

    /**
     * 滑动Item
     *
     * @param position
     */
    public void removeItem(int position) {
        mAuthorInfoList.remove(position);//删除数据
        notifyItemRemoved(position);
    }

    class AuthorViewHolder extends RecyclerView.ViewHolder {

        ImageView mPortraitView;
        TextView mNickNameView;
        TextView mMottoView;

        public AuthorViewHolder(View itemView) {
            super(itemView);

            mPortraitView = (ImageView) itemView.findViewById(R.id.iv_portrait);
            mNickNameView = (TextView) itemView.findViewById(R.id.tv_nickname);
            mMottoView = (TextView) itemView.findViewById(R.id.tv_motto);

        }
    }
}
